package modelDeLivreur;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DirectionsManager {
	
	/**
	 * Static method allowing control of instanciation process of DirectionManager class.
	 * Implementation of Singleto pattern.
	 * 
	 * @return DirectionManager
	 */
	public static DirectionsManager getInstance(){
		if(dm == null){
			dm = new DirectionsManager();
		}
		return dm;
	}
	
	/**
	 * Method that adds new direction object to collection of directions
	 * 
	 * @param htmlDirection
	 * @param duration
	 * @param distance
	 * @return
	 */
	public boolean addDirection(String htmlDirection, String duration, String distance){
		return directions.add(new Direction(htmlDirection, duration, distance));
	}
	
	/**
	 * Method that creates an iterator for traversing directions collection 
	 * @return Iterator<Direction>
	 */
	public Iterator<Direction> iterator(){
		return directions.iterator();
	}
	
	/**
	 * Method calculating total trajectory distance by iterating over collection
	 * of directions for specific trajectory.
	 * 
	 * @return double
	 */
	public double getTotalDistance(){
		int total = 0;
		
		Iterator<Direction> it = iterator();
		for(;it.hasNext();){
			total +=it.next().getNumberDistance();
		}
		return total;
	}
	
	/**
	 * Method calculating total trajectory duration by iterating over collection
	 * of directions for specific trajectory.
	 * 
	 * @return double
	 */
	public double getTotalDuration(){
		int total = 0;
		
		Iterator<Direction> it = iterator();
		for(;it.hasNext();){
			total +=it.next().getNumberDuration();
		}
		return total;
	}
	
	private DirectionsManager(){
		directions = new ArrayList<Direction>();
	}
	
	private List<Direction> directions = null;
	private static DirectionsManager dm = null;
}
